﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Business;
using Business.Entities;
using GogTasks.Entities;

namespace GogTasks.UserControls
{
    public partial class SettingsUserControl : UserControl
    {
        public bool TasksWiped;
        SettingsForm _settingsForm;
        private bool _initDone;


        public SettingsUserControl(SettingsForm sf)
        {
            _settingsForm = sf;
            InitializeComponent();
            BindUI();
            _initDone = true;
        }

        private void AuthButton_Click(object sender, EventArgs e)
        {
            var authForm = new AuthForm(MobileContext.Config.Username);
            int x = (Screen.PrimaryScreen.Bounds.Width - authForm.Width)/2;
            int y = (Screen.PrimaryScreen.Bounds.Height - authForm.Height) / 2;
            authForm.Location = new Point(x, y);
            _settingsForm.EnableButtons(false);
            authForm.ShowDialog();
            BindUI();
            configBindingSource.ResetBindings(false);
            if (!MobileContext.Config.RightHandedCheckBoxes) radioButton1.Checked = true;
            _settingsForm.EnableButtons(true);
        }

        private void BindUI()
        {
            configBindingSource.DataSource = MobileContext.Config;
            //syncIntervalListBindingSource = SyncIntervalList.GetSyncIntervalList();
            CreateListBoxData();

            //if (String.IsNullOrEmpty(Context.Config.Username))
            //    AuthLabel.Text = "(No one)";
            //else AuthLabel.Text = Context.Config.Username;
            radioButton2.Checked = MobileContext.Config.RightHandedCheckBoxes;
            radioButton1.Checked = !MobileContext.Config.RightHandedCheckBoxes; 
        }

        private void CreateListBoxData()
        {
            // Sync interval list box
            AutoSyncComboBox.Items.Clear();
            var dic = SyncIntervalList.GetSyncIntervalList();
            foreach (int key in dic.Keys)
            {
                var kvp = new NilzKvp(key, dic[key]);
                AutoSyncComboBox.Items.Add(kvp);
                if  (MobileContext.Config.AutoSyncIntervalMinutes == key)
                    AutoSyncComboBox.SelectedItem = kvp;
            }
            // Font size
            FontSizeComboBox.Items.Add(FontSizeEnum.Small);
            FontSizeComboBox.Items.Add(FontSizeEnum.Medium);
            FontSizeComboBox.Items.Add(FontSizeEnum.Large);
            FontSizeComboBox.SelectedIndex = (int)MobileContext.Config.FontSize;
        }

        private void Help2Button_Click(object sender, EventArgs e)
        {
            MessageBox.Show(
            "After having authenticated with Google Accounts, an authentication token is "+ 
            "stored on this device in order to relogin automatically every time the program " +
            "synchronizes the task list. This is an encryptet piece of information from Google. " +
            "Your Google password is not stored on this device.", "Google Authentication");
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked) MobileContext.Config.RightHandedCheckBoxes = true;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked) MobileContext.Config.RightHandedCheckBoxes = false;
        }

        private void CloseSyncCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
           MobileContext.Config.AutoSyncOnClose = CloseSyncCheckBox.Checked;
        }

        private void StartupSyncCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            MobileContext.Config.AutoSyncOnStart = StartupSyncCheckBox.Checked;
        }

        private void StartupSyncCheckBox_Click(object sender, EventArgs e)
        {

            //MobileContext.Config.AutoSyncOnClose = checkBox1.Checked;
        }

        private void checkBox1_Click(object sender, EventArgs e)
        {

            //MobileContext.Config.AutoSyncOnStart = checkBox1.Checked;
        }

        private void WipeButton_Click(object sender, EventArgs e)
        {
            var res = MessageBox.Show("Are you sure you wish to delete all tasks on this device?", 
                "Confirm", 
                MessageBoxButtons.OKCancel, 
                MessageBoxIcon.Exclamation, 
                MessageBoxDefaultButton.Button2);
            if (res == DialogResult.OK)
            {
                WipeTasks();
                MessageBox.Show("All tasks deleted successfully", "Wipe complete");
            }
        }

        private void WipeTasks()
        {
            try
            {
                var oApi = new MobileOutlookApi();
                var root = oApi.GetOutlookTasks();
                foreach (var list in root.lists)
                {
                    foreach (var task in list.child_entity)
                    {
                        oApi.DeleteTask(task.OutlookTask);
                    }
                }
                TasksWiped = true;
                SyncHistory.Wipe();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:" + ex.Message, "Error");
            }
        }

        private void MaxPerPageControl_ValueChanged(object sender, EventArgs e)
        {
            // Databinding not workingm, so...
            MobileContext.Config.MaxTasksPerPage = (int) MaxPerPageControl.Value;
        }

        private void HideCompletedCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            MobileContext.Config.HideCompletedTasks = HideCompletedCheckBox.Checked;
        }

        private void AutoSyncComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            var item = AutoSyncComboBox.SelectedItem as NilzKvp;
            MobileContext.Config.AutoSyncIntervalMinutes = item.Key;
        }

        private void AutoSyncBgCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            MobileContext.Config.AutoSyncInBackground = AutoSyncBgCheckBox.Checked;
        }

        private void FontSizeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MobileContext.Config.FontSize = (FontSizeEnum)FontSizeComboBox.SelectedIndex; 
            if (_initDone) TasksWiped = true; // Set this to trigger re-rendering of the tasks
        }

    }
}
